/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-07
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.basics.entity.Vip;
import com.rzico.basics.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * 会员等级控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-07
 */
@Api(description = "会员等级接口")
@RestController
@RequestMapping("/admin/vip")
public class VipAdminController extends BaseController {

    @Autowired
    private VipService vipService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询会员等级
     *
     * @return
    */
    @ApiOperation("分页查询会员等级")
    @GetMapping("/list")
    public CommResult<Vip> list(String startDate, String endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Vip> list = vipService.selectList(params);
        PageResult<Vip> pageResult = new PageResult<Vip>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条会员等级
     *
     * @return
    */
    @ApiOperation("查询单条会员等级")
    @GetMapping("/find/{id}")
    public CommResult<Vip> find(@PathVariable String id) {
        Vip result = vipService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("vip",result);
        return CommResult.success(data);

    }

    /**
     * 保存会员等级
     *
     * @param vip
     * @return
     */
    @ApiOperation("保存会员等级")
    @PostMapping("/save")
    public CommResult<Vip> save(@RequestBody Vip vip) {
        vip.setCreateDate(new Date());
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise==null) {
            return CommResult.error("没有开通企业");
        }
        if (vip.getQuantity()==null) {
            vip.setQuantity(0);
        }
        if (vip.getAmount()==null) {
            vip.setAmount(BigDecimal.ZERO);
        }
        if (vip.getPeople()==null) {
            vip.setPeople(0);
        }
        vip.setEnterpriseId(enterprise.getId());
        int affectCount = vipService.insert(vip);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Vip result = vipService.findById(vip.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除会员等级
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除会员等级", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除会员等级,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Vip> del(@PathVariable String ids) {

        vipService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新会员等级
     *
     * @param vip
     * @return
     */
    @Log(desc = "更新会员等级", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新会员等级")
    @PostMapping("/update")
    public CommResult<Vip> update(@RequestBody Vip vip) {
        if (vip.getQuantity()==null) {
            vip.setQuantity(0);
        }
        if (vip.getAmount()==null) {
            vip.setAmount(BigDecimal.ZERO);
        }
        if (vip.getPeople()==null) {
            vip.setPeople(0);
        }
        int affectCount = vipService.updateByPrimaryKeySelective(vip);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Vip result = vipService.findById(vip.getId());
        return CommResult.success(result);
    }

}
